define(function (require) {
    var TVUI = window.TVUI || require('tvui'),
        model = require('model'),
        $ = TVUI.$,
        page = new TVUI.Page({name: 'femaleWorkersList'}),
        param = TVUI.Util.queryToJson(location.search),
        cache = page.getCache(),
        $conList = $('#conList'),
        $pageMsg = $('#pageMsg'),
        $noData = $('#noData'),
        listSize = 10,//列表的每页个数
        conList, pageCount;

    var adData = ['css/adImg/listAd.png'];

    var femaleWorkersList = {
        //列表限制字数
        listTitleLength: 70,
        init: function () {
            this.setPanel();
            this.bindEvent();
            //有缓存的处理
            this.renderList(cache.listPage || 0, function (listData, pageData) {
                if (listData && listData.length > 0) {
                    conList.reset(listData, pageData.totalElements, cache.listPage || 0, cache.listIndex || 0);
                } else {
                    $noData.html('暂无相关信息');
                }
            });
            page.clearCache();
        },

        //渲染列表
        renderList: function (currentPage, callback) {
            var self = this;
            model.findContents({categoryId: param.id, currentPage: (currentPage + 1), pageSize: listSize}, {localCache: true}).done(function (res) {
                var cacheListData = $.extend(true, [], res.data.contents),
                    cachePageData = res.data.pagination;
                for (var i = 0; i < cacheListData.length; i++) {
                    cacheListData[i].shortTit = TVUI.Util.subStr(cacheListData[i].title, self.listTitleLength);
                }
                pageCount = cachePageData.totalPages;
                callback && callback(cacheListData, cachePageData);
            });
        },
        setPanel: function () {
            var self = this;

            conList = new TVUI.List({
                container: $conList,
                template: '<ul>{{#data}}<li data-id="{{id}}" data-text="{{title}}">{{shortTit}}</li>{{/data}}</ul>',
                cols: 1,
                type: 1,
                async: true,
                pageSize: listSize,
                textChar: self.listTitleLength
            });

            page.addPanel(conList);
        },
        bindEvent: function () {
            var self = this;
            conList.on('pager', function (pageIndex, type) {
                if (type > 0) {
                    self.renderList(pageIndex, function (listData, pageData) {
                        //显示第pageIndex页lists
                        conList.reset(listData, pageData.totalElements, pageIndex, 0);
                        conList.autoFocus(type);
                    });
                }
                if (pageCount > 1) {
                    $pageMsg.show();
                    $pageMsg.html((pageIndex + 1) + '/' + pageCount);
                }
            });

            conList.on('select', function (i, li) {
                page.setCache({listIndex: i, listPage: conList.pager.pageIndex});
                page.go('femaleWorkersArticle.html', {id: $(li).attr('data-id')});
            });
        }
    };

    femaleWorkersList.init();
});